<?php

class Log {

	public static function add($message, $file = NULL){
		
		if(!isset($file)){
			$file = LOGS.'defaultLog.txt';
		}
		else {
			$file = LOGS.$file;
		}
		
		$message = file_get_contents($file).sprintf(
			"------------------------------ %s %s ------------------------------\n".
			"%s".
			"---------------------------------------------------------------------------------\n\n",
			date("d-m-Y"),
			date("H:i:s"),
			$message
		);
		
		file_put_contents($file, $message);
	}

	public static function addException(Exception $e){

		$traceString = '';
		foreach($e->getTrace() as $trace){
			
			if(is_array($trace)){
				foreach($trace as $key => $value){
					$traceString .= '            ';
					if($key == 'args'){
						$traceString .= str_pad($key, 10, ' ').': ';
						foreach($value as $key => $argument){
							if($key != 0){
								$traceString .= '                        ';
							}
							$traceString .= $argument;
							$traceString .= "\n";
						}
					}
					else {
						$traceString .= str_pad($key, 10, " ").': '.$value."\n";
					}
				}
			}	
		}	
		//echo '<pre>'; print_r($traceString); echo '</pre>';
				
		$message =
			str_pad('file', 10, " ").": ".$e->getFile()."\n".
			str_pad('message', 10, " ").": ".$e->getMessage()."\n".
			str_pad('line', 10, " ").": ".$e->getLine()."\n".
			str_pad('trace', 10, " ").": \n".$traceString."\n"
		;
		
		self::add($message, 'exceptions.txt');
		//echo '<pre>'; print_r($message); echo '</pre>';
		
	}
	
}

?>